package com.ddtech.business.common;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.ddtech.business.constant.Del;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

/**
 * 公共基础实体类
 *
 * @author bin
 * @date 2022/2/21 13:17
 */
@Getter
@Setter
public class BaseEntity {

    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    @ApiModelProperty(value = "主键")
    private String id;

    @ApiModelProperty(value = "是否删除0,1")
    private Integer isDelete;

    @ApiModelProperty(value = "创建时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @ApiModelProperty(value = "更新时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    public void init() {
        if (StringUtils.isBlank(this.id)) {
            this.baseInsert();
        } else {
            this.baseUpdate();
        }
    }

    public void baseInsert() {
        this.isDelete = Del.FALSE.getStatus();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    public void baseUpdate() {
        this.updateTime = LocalDateTime.now();

    }

    public void baseDelete() {
        this.isDelete = Del.TRUE.getStatus();
        this.updateTime = LocalDateTime.now();
    }

    public static final String ID = "id";
    public static final String IS_DELETE = "is_delete";
    public static final String CREATE_TIME = "create_time";
    public static final String UPDATE_TIME = "update_time";
}
